package com.yanchangapp.widget;

import android.app.Dialog;
import android.content.Context;
import android.os.IBinder;
import android.support.annotation.NonNull;
import android.text.InputFilter;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;
import android.widget.TextView;

import com.yanchangapp.R;
import com.yanchangapp.util.CashierInputFilter;
import com.zxl.baselib.util.formatter.BigDecimalUtils;
import com.zxl.baselib.util.ui.UIUtils;

/**
 * Created by apple on 2019-09-09.
 * description:商品价格的dialog
 */
public class GoodsPriceDialog extends Dialog {

    public GoodsPriceDialog(@NonNull Context context) {
        super(context);
    }

    public GoodsPriceDialog(@NonNull Context context, int themeResId) {
        super(context, themeResId);
    }

    public interface PriceEnsureListener{
        void onEnsureClick(double price);
    }

    @Override
    public void dismiss() {
        InputMethodManager imm = (InputMethodManager) getContext().getApplicationContext()
                .getSystemService(Context.INPUT_METHOD_SERVICE);
        // 即使当前焦点不在editText，也是可以隐藏的。
        if (imm != null) {
            final View currentFocus = getCurrentFocus();
            IBinder windowToken = null;
            if (currentFocus != null) {
                windowToken = currentFocus.getWindowToken();
            }
            if (windowToken != null) {
                imm.hideSoftInputFromWindow(windowToken, 0);
            }
        }
        super.dismiss();
    }

    public static class Builder{
        private double price;
        private Context mContext;
        private PriceEnsureListener mPriceEnsureListener;

        public Builder(Context context){
            mContext = context;
        }

        public Builder setPrice(double price){
            this.price = price;
            return this;
        }

        public Builder setEnsureListener(PriceEnsureListener ensureListener){
            this.mPriceEnsureListener = ensureListener;
            return this;
        }

        public GoodsPriceDialog build(){
            final GoodsPriceDialog goodsPriceDialog = new GoodsPriceDialog(mContext, R.style.MyDialog);
            View localView =  LayoutInflater.from(mContext).inflate(R.layout.dialog_set_price,null);
            TextView mTvCancel =  localView.findViewById(R.id.tvCancel);
            TextView mTvEnsure = localView.findViewById(R.id.tvEnsure);
            EditText etNum = localView.findViewById(R.id.etNum);
            String text = BigDecimalUtils.keep2Decimal(price);
            etNum.setText(text);
            etNum.setSelection(text.length());
            InputFilter[] filters={new CashierInputFilter()};
            etNum.setFilters(filters);
            mTvCancel.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    goodsPriceDialog.dismiss();
                }
            });

            mTvEnsure.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    String strPrice = etNum.getText().toString();
                    double price = Double.parseDouble(strPrice);
                    String toStr =  BigDecimalUtils.keep2Decimal(price);
                    if (toStr.length() > 9){
                        UIUtils.showToast("金额不能超过999999.99");
                        return;
                    }

                    if (mPriceEnsureListener == null){
                        goodsPriceDialog.dismiss();
                    }else {
                        mPriceEnsureListener.onEnsureClick(price);
                    }

                }
            });
            goodsPriceDialog.setContentView(localView);

            return goodsPriceDialog;
        }

    }
}
